package ooday03;
//引用类型数组的演示
public class RefArrayDemo {
    public static void main(String[] args) {
        /*
        引用类型数组
            引用类型数组：数组元素为引用类型的数组
                存储10名学生：Student[] student = new Student[10];
                存储3只学小狗：Dog[] dog = new Dog[3];
                存储5辆汽车：Car[] car = new Car[5];

                //声明Dog型数组，包含3个元素
                //每个元素都是Dog型，默认值为null
                Dog[] dogs = new Dog[3];
         */

        /*
        //声明Dog型数组dogs，包含3个元素，每个元素都是Dog类型，默认值为null
        Dog[] dogs = new Dog[3];
        //声明Chick型数组chicks，包含3个元素，每个元素都是Chick类型，默认值为null
        Chick[] chicks = new Chick[3];
        //声明Fish型数组fish，包含3个元素，每个元素都是Fish类型，默认值为null
        Fish[] fish = new Fish[3];
         */

        /*
        引用类型数组特点
            引用类型数组与基本类型数组，有两点区别：
                1)给引用类型数组的元素赋值时，需要new个对象
                2)访问引用类型数组元素的属性/行为时，需要打点访问

                int[] arr = new int[3]; //基本类型数组
                arr[0] = 100;
         */
        /*
        //1)给引用类型数组的元素赋值时，需要new个对象
        //演示引用类型数组与基本类型数组的第1点区别
                Dog[] dogs = new Dog[3]; //引用数组类型   -----创建Dog数组对象
                //dogs[0] = 100;
                dogs[0] = new Dog("小黑",2,"黑");//创建Dog对象
                dogs[1] = new Dog("小白",1,"白");
                dogs[2] = new Dog("小灰",3,"灰");

                Chick[] chicks = new Chick[2];
                chicks[0] = new Chick("小花",1,"花");
                chicks[1] = new Chick("大花",2,"花");

                Fish[] fish = new Fish[4];
                fish[0] = new Fish("小金",2,"金");
                fish[1] = new Fish("大金",3,"金");
                fish[2] = new Fish("小绿",1,"绿");
                fish[3] = new Fish("小红",3,"红");
         */
        //2)访问引用类型数组元素的属性/行为时，需要打点访问
        //演示引用类型数组与基本类型数组的第2点区别
            Dog[] dogs = new Dog[3]; //null,null,null
            dogs[0] = new Dog("小黑",2,"黑");
            dogs[1] = new Dog("小白",1,"白");
            dogs[2] = new Dog("小灰",3,"灰");
            System.out.println(dogs[0].name);//输出第1只狗狗的名字
            dogs[1].age = 4;//修改第2只狗狗的年龄为4岁
            dogs[2].swim();//第3只狗狗在游泳
            for (int i=0;i<dogs.length;i++){//遍历dogs数组
                System.out.println(dogs[i].name);//输出每只狗狗的名字
                dogs[i].eat();//每只狗狗吃饭
            }

            Chick[] chicks = new Chick[2];
            chicks[0] = new Chick("小花",1,"花");
            chicks[1] = new Chick("大花",2,"花");
            for (int i=0;i<chicks.length;i++){//遍历Chick数组
                System.out.println(chicks[i].name);//输出每只小鸡的名字
                chicks[i].eat();//每只小鸡吃饭
            }


            Fish[] fish = new Fish[4];
            fish[0] = new Fish("小金",2,"金");
            fish[1] = new Fish("大金",3,"金");
            fish[2] = new Fish("小绿",1,"绿");
            fish[3] = new Fish("小红",3,"红");
            for (int i=0;i<fish.length;i++){//遍历fish数组
                System.out.println(fish[i].name);//输出每条鱼的名字
                fish[i].eat();//每只小鱼吃饭
            }

        /*
        补充：
            1.null：表示空，没有只想任何对象
                    若引用的值为null，则该引用不能在进行任何操作了，若操作则发生NullPointerException空指针异常
        //框架-------Spring
        */

    }

}










